package com.android.demolayoutpages;

 

import android.content.Context;
 
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.PorterDuff.Mode;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

public class ExpertsImageAdapter extends BaseAdapter {
	private static final String LOG_TAG = "MenuImageAdapter";
	 

	private Context context;
	private int width;
	private int height;



	public ExpertsImageAdapter(Context context) {
		this.context=context;

	}

	public int getCount() {
		return 12;
	}

	public Object getItem(int arg0) {


		return null;
	}

	public long getItemId(int arg0) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
		View view;
		if (convertView==null) {
			//setUpLayout(position);

			WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
			Display display = wm.getDefaultDisplay();
			width  = display.getWidth();
			height = display.getHeight();
			int gridLayoutWidth  =  width*6/10;
			int gridLayoutHeight =  height*6/10;
			int gridWidth =  (int) (gridLayoutWidth/(2.4));
			int gridHeight = (int) (gridLayoutHeight/2.4);
			int gridImageWidth  = gridWidth*9/10;
			int gridImageHeight = gridWidth*9/10;
			 


			view = inflater.inflate(R.layout.layout_expert, null);
			view.setLayoutParams(new GridView.LayoutParams(gridWidth, gridHeight));
			
			ImageView ivExperts = (ImageView)view.findViewById(R.id.img_expert);
			ivExperts.setPadding(2, 2,2,2);
			Bitmap bm = getRoundedRectBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.img_profile), 200);
			ivExperts.setImageBitmap(bm );
			ivExperts.setBackgroundResource(R.drawable.circle_shape_expert_image);
		//	gvGifts.setColumnWidth((int) (width/3.5));

			 
			//ImageView imgView = (ImageView)view.findViewById(R.id.imageView_rounded);
			//TextView txtView = (TextView)view.findViewById(R.id.textView1);
			 
			//imgView.setLayoutParams(new LinearLayout.LayoutParams(gridImageWidth, gridImageHeight));
			//imgView.setBackgroundResource(R.drawable.circle_shape);
			 
			 
		}else

			view=convertView;
		return view;
	}


	 

	public Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
		Bitmap result = null;
		try {
			result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
					Bitmap.Config.ARGB_8888);
			Canvas canvas = new Canvas(result);

			int color = 0xff424242;
			Paint paint = new Paint();
			Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
			RectF rectF = new RectF(rect);
			int roundPx = pixels;

			paint.setAntiAlias(true);
			canvas.drawARGB(0, 0, 0, 0);
			paint.setColor(color);
			canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas.drawBitmap(bitmap, rect, rect, paint);
		} catch (NullPointerException e) {
			// return bitmap;
		} catch (OutOfMemoryError o) {
		}
		return result;
	}





	public static  class ViewHolder{
		static LinearLayout layout;
		static ImageView imageView;
		static TextView textView;
	}

}
